from rest_framework import serializers

from home.models import NavCategory, Kecheng, Banner


class NavCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = NavCategory
        fields = ['id', 'name', 'display_name', 'weight', 'external_link']


class KechengSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()

    class Meta:
        model = Kecheng
        fields = ["id", "name", "level", "description", "parent", "children"]

    def get_children(self, obj):
        children = Kecheng.objects.filter(parent=obj)
        if children:
            ser = KechengSerializer(children, many=True)
            return ser.data
        else:
            return []


class BannersSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = ['id', 'title', 'image', 'link', 'note']

